<?php
$question_num = 25;

function random_from($min,$max,$num){
    $count = 0;
    $return = array();
    while ($count < $num) {
        $return[] = mt_rand($min, $max);
        $return = array_flip(array_flip($return));
        $count = count($return);
    }
    shuffle($return);
    return $return;
}

class Q_single{
    var $question,$ans,$ans_id;
    function ans_shuffle(){
        shuffle($this->ans_id);
        for ($i=0;$i<4;$i++){
            $ans_tmp[$i] = $this->ans[$this->ans_id[$i]-1]; 
        }
        $this->ans = $ans_tmp;
    }
    function __construct($question,$ans1,$ans2,$ans3,$ans4,$author,$withpic,$pic_url){
        $this->question = $question;
        $this->ans = array($ans1,$ans2,$ans3,$ans4);
        $this->ans_id = array(1,2,3,4);
        $this->ans_shuffle();
        $this->author = $author;
        if($withpic == '1'){
            $this->withpic = true;
            $this->pic_url = $pic_url;
        }
    }
    
}

function generate_set($mode){
    include 'tools/tools.php';
    $db = local_db();
    if ($mode == 'easy'){
        $sheet = 'questions';
    }else if($mode == 'hard'){
        $sheet = 'questions_hard';
    }
    global $question_num;
    $set = array();
    $sql = "SELECT COUNT(*) FROM `$sheet`";
    $total = $db->query($sql)->fetch_assoc()['COUNT(*)'];
    $indexs = random_from(1,$total,$question_num);
    for ($i=0;$i<$question_num;$i++){
        $sql = "SELECT * FROM `$sheet` INNER JOIN `questions_con` ON `$sheet`.`contribute_No`=`questions_con`.`No` WHERE `$sheet`.`QNo`=$indexs[$i]";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $set[$i] = new Q_single($row['q'],$row['ans1'],$row['ans2'],$row['ans3'],$row['ans4'],$row['author'],$row['withpic'],$row['pic_addr']);
            }
        } else {
            $set[$i] = new Q_single('cannot find question.','qaq','qaq','qaq','qaq','qaq',0,'');
        }
    }
    return array($indexs,$set,$total);
}

function generate_set_scaled($mode){

    #proportion define

    $proportion_list = array(
        "Kanon"=>0.2,
        "Air"=>0.2,
        "Clannad_TA"=>0.2
    );

    include 'tools/tools.php';
    $db = local_db();
    if ($mode == 'easy'){
        $sheet = 'questions';
    }else if($mode == 'hard'){
        $sheet = 'questions_hard';
    }
    global $question_num;
    $set = array();
    $sql = "SELECT COUNT(*) FROM `$sheet`";
    $total = $db->query($sql)->fetch_assoc()['COUNT(*)'];
    $indexs = random_from(1,$total,$question_num);
    for ($i=0;$i<$question_num;$i++){
        $sql = "SELECT * FROM `$sheet` INNER JOIN `questions_con` ON `$sheet`.`contribute_No`=`questions_con`.`No` WHERE `$sheet`.`QNo`=$indexs[$i]";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $set[$i] = new Q_single($row['q'],$row['ans1'],$row['ans2'],$row['ans3'],$row['ans4'],$row['author'],$row['withpic'],$row['pic_addr']);
            }
        } else {
            $set[$i] = new Q_single('cannot find question.','qaq','qaq','qaq','qaq','qaq',0,'');
        }
    }
    return array($indexs,$set,$total);
}

?>